/**
 *  *2010-7-10 下午02:54:37  add 
 */
package com.base.myproject.client.HR.gcode;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.HR.bcode.BcodeSelectDialog;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncGetModelDatas;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.QueryForm;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Administrator
 *2010-7-10
 * javazgw@gmail.com
 */
/**
 * 
 * 根据人员查找人员与物品的情况
 */

public class QueryGoodsBcode extends LayoutContainer{
	BcodeSelectDialog<TreeNodeModelData> bsd_add ;
	QueryForm qf = new QueryForm();
	public QueryGoodsBcode()
	{
		init();
	}
	private void init() {
		createGrid();
		initQueryForm();
		initWidgetx();
		
	}
	private void initWidgetx() {
		VerticalPanel v = new VerticalPanel();
		v.add(qf);
		v.add(goodsgrid);
		this.add(v);
		
	}
	
	void initQueryForm()
	{
		 
		 bsd_add = new BcodeSelectDialog<TreeNodeModelData>(){
				public void doposted(List<TreeNodeModelData> modeldatalist) {
					if(modeldatalist.size()>0)
					{
						this.setEmptyText("已经选择了数据");
					
					}
				}

			};
			
			bsd_add.setNeesreselect(false);
			bsd_add.setEmptyText("未选择数据");
			bsd_add.setName("部门人员");
			bsd_add.setCheckNodes(CheckNodes.BOTH);
			qf.setCollapsible_atquery(true);
		qf.addField("BCODE",bsd_add );
		qf.getQuerybutton().addListener(Events.Select, getListener());
		qf.initcomplete();
		
	}
	BaseStore<BusiObject> goodsstore = new BaseStore<BusiObject>();
	void createstore()
	{
		
	}
	 BaseGrid<BusiObject> goodsgrid;
	void createGrid()
	{
		
		CheckBoxSelectionModel<BusiObject> cbs2 = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns2 = new ArrayList<ColumnConfig>();
		columns2.add(cbs2.getColumn());

//		ColumnConfig C1 = new ColumnConfig("code", "code", 80);
//		columns.add(C1);
//		ColumnConfig goodsC2 = new ColumnConfig("goodscode", "编码", 80);
//		columns2.add(goodsC2);
		ColumnConfig goodsC2 = new ColumnConfig("bname", "人员", 80);
		columns2.add(goodsC2);
		ColumnConfig goodsC7 = new ColumnConfig("name", "名称", 80);
		columns2.add(goodsC7);
		
	//	ColumnConfig goodsC8 = new ColumnConfig("qty", "数量", 80);
//		NumberField C_num6 = new NumberField();
//		C_num6.setPropertyEditorType(Integer.class);
//		
//		goodsC8.setEditor(new CellEditor(C_num6));
	//	columns2.add(goodsC8);
		
		
		ColumnConfig goodsC10 = new ColumnConfig("qtyused", "已领取数量", 80);
		columns2.add(goodsC10);
		ColumnConfig goodsC9 = new ColumnConfig("qtyunit", "单位", 80);
		columns2.add(goodsC9);

		ColumnModel cm2 = new ColumnModel(columns2);
		goodsgrid = new BaseGrid<BusiObject>(goodsstore, cm2, false, false);
		 cbs2.setSelectionMode(SelectionMode.MULTI);
		 goodsgrid.setSelectionModel(cbs2);

		 goodsgrid.setHeight(300);
		 goodsgrid.setWidth(433);
		 goodsgrid.getView().setForceFit(true);
	}
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals( qf.getQuerybutton())) {                            
								query();             
							}                                                      
						} else if (be.getType() == Events.Change) {              
							if (be.getSource().equals("")) {         
                                                                     
							}                                                      
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}     
	
	void query()
	{
//		bsd_add.getTree().getCheckedSelection().
		GWT.log(bsd_add.getTree().getCheckedSelection().size()+"");
		if(bsd_add.getTree().getCheckedSelection().size()<1)
		{
			MessageBox.alert("提示", "请选择部门人员", null);
			return;
		}
		
		qf.doquery();
		CommandSyncContainer list2 = new CommandSyncContainer();

		CommandSyncGetModelDatas csgmd2 = new CommandSyncGetModelDatas();
		csgmd2.setSqlname("hr_goods_query");
		
		String bcode = "";
		//select bcode from systembcode_view where systembcode like '001%'
		for(TreeNodeModelData  bo:bsd_add.getTree().getCheckedSelection())
		{
			bcode +=" or systembcode like '"+bo.get("systembcode")+"%'";
		}
		
			
		bcode = " bcode in (select bcode from systembcode_view where (1=2 " +bcode +" ))";

		csgmd2.putmacro("FILTER",bcode);
		//csgmd.putmacro("SCARDNO", s_cardno.getValue());
		list2.add(csgmd2);
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2
				.SendCommandSync(
						list2,
						new AsyncCallback<CommandSyncReturnObject>() {

							@Override
							public void onFailure(
									Throwable caught) {
								MessageBox.alert("错误",
										"网络连接失败", null);

							}

							@Override
							public void onSuccess(
									CommandSyncReturnObject result) {
								if (result.isB()) {
								
									if (result
											.getCommandsync() instanceof CommandSyncGetModelDatas) {
										
										goodsstore.removeAll();
										goodsstore.add(((CommandSyncGetModelDatas)(result.getCommandsync())).getList(),false);
										
										goodsgrid.getView().scrollToTop();
										
									}

								} else {
									MessageBox
											.alert(
													"错误",
													""
															+ result
																	.getMessage(),
													null);

								}

							}

						});
             
	}
}
